<?php

namespace app\common\model;

use app\common\util\ErrorCode;
use app\common\util\Tools;

/**
 * Class Auth
 * @package app\common\model
 * 表的通用model类
 */
class commonModel
{
    // 操作数据库的数据检查
    public static function pre_data_check($name, $data){
        $result = Tools::checkDataBaseFields(db()->getTable($name), $data);
        if(isset($result["code"])){
            return $result;
        }
        return true;
    }

    //分页获取所有字段数据
    public static function getList($name, $field = '*', $filter = [], $page = 1, $limit = 10, $order = "", $paginate = true){
        $db = db($name)->where($filter)->field($field);
        if(!empty($order)){
            $db->order($order);
        }
        if($paginate){
            $data = $db->paginate(array('page' => $page, 'list_rows' => $limit));
            if($data){
                $data = $data->toArray();
                $data["data"] = Tools::transferFields($data["data"]);
                return $data;
            }
        }else{
            if($page && $limit)
                $db->limit($page-1, $limit);
            $data = $db->select();
            $data = Tools::transferFields($data);
            return $data;
        }
        return [];
    }

    public static function getFields($name, $field, $filter = [], $one = true, $fetch = true){
        $db = db($name)->where($filter)->field($field);
        if($one){
            $data = $db->find();
        }else{
            $data = $db->select();
        }
        if(!empty($data)) {
            $data = Tools::transferFields($data);
        }else if($fetch){
            if($field == "*") {
                $field = db()->getTableFields(db()->getTable($name));
                foreach (Tools::transferFields(array_flip($field)) as $v => $k){
                    $data[$v] = '';
                }
            }else{
                if(!is_array($field)){
                    $field = explode(",", $field);
                }
                foreach (Tools::transferFields(array_flip($field)) as $v => $k){
                    $data[$v] = '';
                }
            }
        }else{
            $data = [];
        }
        return $data;
    }

    //根据条件获取数量
    public static function getCount($name, $filter = []){
        if(!empty($filter)){
            return db($name)->where($filter)->count();
        }else{
            return db($name)->count();
        }
    }

    public static function getColumn($name, $field, $filter = [], $key = ''){
        if(!is_array($field)){
            return db($name)->where($filter)->column($field, $key);
        }
        return [];
    }

    public static function toAdd($name, $data, $getId = false){
        if(!$data)
            return true;
        try {
            $result = self::pre_data_check($name, $data);
            if (isset($result["code"])) {
                return $result;
            }
            if ($getId) {
                return db($name)->insertGetId($data);
            } else {
                return db($name)->insert($data);
            }
        }catch (\Exception $e){
            return ErrorCode::code("try_error", $e->getMessage());
        }
    }

    public static function toUpdate($name, $data, $filter){
        if(!$data)
            return true;
        $result = self::pre_data_check($name, $data);
        if(isset($result["code"])){
            return $result;
        }
        $result = db($name)->where($filter)->update($data);
        if($result || $result === 0){
            return true;
        }
        return false;
    }

    //根据ID删除数据
    public static function delById($name, $id){
        if(is_array($id)){
            $result = db($name)->where('id', 'in', $id)->delete();
        }else{
            $result = db($name)->where('id', $id)->delete();
        }
        return $result;
    }

    //根据条件删除数据
    public static function delByFilter($name, $where){
        $result = db($name)->where($where)->delete();
        return $result;
    }
}